package com.flong.spring;


import com.flong.spring.annotation.StudentConfig;
import com.flong.spring.annotation.StudentConfig2;
import com.flong.spring.bean.Blue;
import com.flong.spring.bean.Student;
import com.flong.spring.config.BeanLifeCycle;
import com.flong.spring.config.ConfigOfAutowired;
import com.flong.spring.config.ConfigOfProfile;
import com.flong.spring.config.ConfigOfPropertyValues;
import com.flong.spring.dao.StudentDao;
import com.flong.spring.service.StudentService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.Environment;

import java.util.Map;

/**
 * @Author:liangjl
 * @Date: 2020/6/8-22:22
 * @Eamil:jilongliang@sina.com
 * @Description: IOC (Inverse of Control ,控制反转）
 * DI (Dependency Injection 依赖注入）
 */
public class SpringClient1 {
    @Test
    public void test0() {
        //获取资源文件
        //ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //Student student = applicationContext.getBean("student", Student.class);
        //System.out.println(student.getUserName() + "--" + student.getAge());
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(StudentConfig.class);
        Student student = applicationContext.getBean(Student.class);
        System.out.println(student.getUserName() + "--" + student.getAge());
    }


    @Test
    public void test1() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(StudentConfig.class);
        String[] definitionNames = applicationContext.getBeanDefinitionNames();

        for (String definitionName : definitionNames) {
            System.out.println(definitionName);
        }
    }

    @Test
    public void test2() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(StudentConfig2.class);
        System.out.println("ico容器创建完成..");
        Object student1 = applicationContext.getBean("student1");
        Object student2 = applicationContext.getBean("student1");
        System.out.println(student1 == student2);

    }

    @Test
    public void test3() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(StudentConfig2.class);
        String[] studentArr = applicationContext.getBeanNamesForType(Student.class);
        for (String stu : studentArr) {
            System.out.println(stu);
        }

        Map<String, Student> beansOfType = applicationContext.getBeansOfType(Student.class);
        System.out.println(beansOfType);

        //获取环境变量的操作系统
        Environment environment = applicationContext.getEnvironment();
        String osName = environment.getProperty("os.name");
        System.out.println(osName);

    }


    @Test
    public void test4() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(StudentConfig2.class);

        for (String beanDefinitionName : applicationContext.getBeanDefinitionNames()) {
            System.out.println(beanDefinitionName);
        }

        Blue blue = applicationContext.getBean(Blue.class);
        System.out.println(blue);

        Object colorFactoryBean = applicationContext.getBean("colorFactoryBean");
        System.out.println(colorFactoryBean.getClass());

        Object colorFactoryBean1 = applicationContext.getBean("&colorFactoryBean");

        System.out.println(colorFactoryBean1 == colorFactoryBean);
    }


    @Test
    public void test5() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanLifeCycle.class);

        for (String beanDefinitionName : applicationContext.getBeanDefinitionNames()) {
            System.out.println(beanDefinitionName);
        }
    }


    @Test
    public void test6() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ConfigOfPropertyValues.class);
        for (String beanDefinitionName : applicationContext.getBeanDefinitionNames()) {
            System.out.println(beanDefinitionName);
        }
        Student stu = applicationContext.getBean("getStu", Student.class);
        System.out.println(stu.getUserName() + ":" + stu.getAge() + ":" + stu.getAddress());

        //从环境变量接口获取配置值
        Environment environment = applicationContext.getEnvironment();
        String address = environment.getProperty("address");
        System.out.println(address);

    }


    @Test
    public void test7() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ConfigOfAutowired.class);
        for (String beanDefinitionName : applicationContext.getBeanDefinitionNames()) {
            System.out.println(beanDefinitionName);
        }

        StudentService studentService = applicationContext.getBean(StudentService.class);
        System.out.println(studentService);

        StudentDao studentDao = applicationContext.getBean(StudentDao.class);
        System.out.println(studentDao);

        applicationContext.close();
    }


    @Test
    public void test8() {
        /**
         * 1、运行指定 VM options 为 -Dspring.profiles.active=test ，此时会运行test环境
         * 2、代码进行设置激活
         * AnnotationConfigApplicationContext不能指定类进行.class
         * 不然会报错 java.lang.IllegalStateException: GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once
         */
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

        //设置激活的环境参数
        applicationContext.getEnvironment().setActiveProfiles("dev");

        //注册到此环境
        applicationContext.register(ConfigOfProfile.class);

        //启动刷新器
        applicationContext.refresh();


        for (String beanDefinitionName : applicationContext.getBeanDefinitionNames()) {
            System.out.println(beanDefinitionName);
        }
        applicationContext.close();
    }
}
